using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using T2V.SharePoint.Common.Utilities;
using Pesico.Forms.Common;
using System.Reflection;
using T2V.SharePoint.Common.Helpers;
using T2V.SharePoint.Common.Models;
using T2V.SharePoint.Common.Extensions;

namespace Pesico.Forms.Features.Pesico.Forms.Web
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("236f417e-fd6f-420a-8fa8-738c1bb97715")]
    public class PesicoFormsEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            try
            {

                ConfigLists(web);

            }
            catch (Exception ex)
            {
                Utility.LogError(ex.Message, "PesicoFormsEventReceiver");
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        #region Methods
        private void ConfigLists(SPWeb web)
        {
            try
            {
                web.AllowUnsafeUpdates = true;

                SPList listPurchase = Utility.GetListFromURL(Constants.CONTRACT_LIST_URL, web);
                if (listPurchase != null)
                {
                    CreateApprovalWorkflow(listPurchase, "Approval Workflow", "Contract Approval", "Pesico.Forms.XMLCustomSettings.DemoWorkflow.xml");
                }
            }
            catch (Exception ex)
            {
                Utility.LogError(ex.Message, "PesicoFormsEventReceiver ConfigLists");
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }

        private void CreateApprovalWorkflow(SPList list, string workflowTemplateName, string workflowName, string fileSettings)
        {
            string association = string.Empty;

            if (!string.IsNullOrEmpty(fileSettings))
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                string xml = assembly.GetResourceTextFile(fileSettings);

                association = SerializationHelper.SerializeToXml(SerializationHelper.DeserializeFromXml<ApprovalWFAssociationData>(xml));
            }

            list.AssociateWorkflow(workflowTemplateName, workflowName, association, "Tasks");
        }
        #endregion Methods
    }
}
